const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");

const app = express();

// 参考文档: https://codeforgeek.com/2014/09/handle-get-post-request-express-4/
// 可以解析 request payload, 即 res.body
app.use(bodyParser.json());

app.use(function (req, res, next) {
	const path = req.originalUrl;
	next();
});

app.use(function (req, res, next) {
		res.header('Access-Control-Allow-Origin', '*');
		res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
		next();
	}
);

app.get("/test", function (req, res) {
	res.status(200).json({
		"msg": "ok"
	})
});

app.use("/static/", express.static(path.join(__dirname, "../static")));

// API
app.use(require("./data-fetcher/index"));

module.exports = app;
